package longrise.phone.com.bjjt_jyb.myaccident;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.FrameworkManager;
import com.longrise.android.LFView;

import longrise.phone.com.bjjt_jyb.Application.BaseApplication;
import longrise.phone.com.bjjt_jyb.R;
import longrise.phone.com.bjjt_jyb.Utils.UiUtil;
import longrise.phone.com.bjjt_jyb.Utils.UiUtils;
import longrise.phone.com.bjjt_jyb.compensate.WebViewLoad;


/**
 * 事故详情界面
 */
public class AccidentDetailView extends LFView implements View.OnClickListener {

    private Context mContext;

    private View mView;

    private EntityBean baseBean;

    private EntityBean dataBean;

    private TextView otherInfoTV;

    private TextView myInfoTV;

    private TextView respPicTV;

    private TextView cTimeTV;

    private TextView stateTV;

    private TextView addTV;

    private BaseApplication application;

    private EntityBean otherBean;//对方信息
    private BaseApplication mApplication;
    private EntityBean myBean;//我的信息
    private TextView adetail_appcaseno;
    private TextView tv_web;
    private String appcaseno;
    private String isSingle;

    public AccidentDetailView(Context context, EntityBean baseBean, EntityBean dataBean) {
        super(context);
        this.mContext = context;
        this.baseBean = baseBean;
        this.dataBean = dataBean;
        this.mApplication = (BaseApplication)context.getApplicationContext();
    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter form = new FormParameter();
        form.setShowtitle(false);
        return form;
    }

    @Override
    public View getView() {
        return mView;
    }

    @Override
    public void init() {
        if (null == mContext) {
            return;
        }
        mView = LayoutInflater.from(mContext).inflate(R.layout.view_accidentdetail, null);
        if (mView == null) {
            return;
        }
        //标题栏
        TextView titleTV = (TextView) mView.findViewById(R.id.tv_title);
        titleTV.setText(R.string.accident_detail_info);
        mView.findViewById(R.id.hn_btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeForm();
                OnDestroy();
            }
        });
        //提交时间
        cTimeTV = (TextView) mView.findViewById(R.id.adetail_tv_ctime);
        tv_web = (TextView) mView.findViewById(R.id.tv_web);
        adetail_appcaseno = (TextView) mView.findViewById(R.id.adetail_appcaseno);//案件编号
        //事故状态
        stateTV = (TextView) mView.findViewById(R.id.adetail_tv_state);
        //事故地点
        addTV = (TextView) mView.findViewById(R.id.adetail_tv_add);
        otherInfoTV = (TextView) mView.findViewById(R.id.adetail_tv_oinfo);
        myInfoTV = (TextView) mView.findViewById(R.id.adetail_tv_minfo);
        respPicTV = (TextView) mView.findViewById(R.id.adetail_tv_responsepic);
        setListener(true);
        application = (BaseApplication) mContext.getApplicationContext();
        setData();
    }

    public void setListener(boolean flag) {
        if (null != otherInfoTV) {
            otherInfoTV.setOnClickListener(flag ? this : null);
        }
        if (null != myInfoTV) {
            myInfoTV.setOnClickListener(flag ? this : null);
        }
        if (null != respPicTV) {
            respPicTV.setOnClickListener(flag ? this : null);
        }
        if (null != tv_web) {
            tv_web.setOnClickListener(flag ? this : null);
        }
    }

    private void setData() {
        if (null == dataBean) {
            return;
        }
        //单车隐藏对方信息栏
        isSingle = baseBean.getString("casetype");
        if (TextUtils.equals("0", isSingle)) {
            mView.findViewById(R.id.adetail_layout_oinfo).setVisibility(View.GONE);
            myInfoTV.setText("单车事故信息");
            mView.findViewById(R.id.ll_rending).setVisibility(View.GONE);
        }
        //时间
        String time = dataBean.getString("casehaptime");
        if (TextUtils.isEmpty(time)) {
            time = "";
        } else {
            int index = time.indexOf(".");
            if (index > -1) {
                time = time.substring(0, index);
            }
        }
        cTimeTV.setText(time);
        //状态
        String state = baseBean.getString("state");
        stateTV.setText(getStateStr(state));
        //地点
        String address = dataBean.getString("caseaddress");
        addTV.setText(address);
        //案件编号
        appcaseno = baseBean.getString("casenumber");
        adetail_appcaseno.setText(appcaseno);
        getAccidentInfo();
    }

    //获取案件状态
    private String getStateStr(String state) {
        if (TextUtils.equals(state, "1")) {
            return "待定责";
        } else if (TextUtils.equals(state, "2")) {
            return "待保险报案";
        } else if (TextUtils.equals(state, "3")) {
            return "待理赔";
        } else if (TextUtils.equals(state, "4")) {
            return "待评价";
        } else if (TextUtils.equals(state, "5")) {
            return "完成";
        } else if (TextUtils.equals(state, "6")) {
            return "撤销案件";
        } else if (TextUtils.equals(state, "7")) {
            return "定责图片待审";
        } else if (TextUtils.equals(state, "8")) {
            return "线下理赔";
        } else if (TextUtils.equals(state, "9")) {
            return "案件信息未上传";
        } else if (TextUtils.equals(state, "10")) {
            return "未生成认定书";
        } else if (TextUtils.equals(state, "11")) {
            return "定损待审";
        } else if (TextUtils.equals(state, "12")) {
            return "未上传银行卡信息";
        } else if (TextUtils.equals(state, "13")) {
            return "定损照片未上传";
        } else if (TextUtils.equals(state, "15")) {
            return "放弃理赔";
        }
        return "未知";
    }

    /**
     * 获取事故车辆相关的信息
     */
    private void getAccidentInfo() {
        EntityBean[] aBeans = dataBean.getBeans("casecarlist");
        if (aBeans == null || aBeans.length < 1) {
            return;
        }
        for (int i = 0; i < aBeans.length; i++) {
            String ownother = aBeans[i].getString("ownother");
            if (TextUtils.equals(ownother, "0")) {
                myBean = aBeans[i];
            } else if (TextUtils.equals(ownother, "1")) {
                otherBean = aBeans[i];
            }
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.adetail_tv_oinfo://甲方信息
                if (otherBean == null) {
                    UiUtil.showToast(mContext, "暂无信息");
                    return;
                }
                AccidentOtherView otherView = new AccidentOtherView(mContext, otherBean,isSingle);
                showForm(otherView);
                break;
            case R.id.adetail_tv_minfo://乙方信息
                if (myBean == null) {
                    UiUtil.showToast(mContext, "暂无信息");
                    return;
                }
                AccidentMyView myView = new AccidentMyView(mContext, myBean,isSingle);
                showForm(myView);
                break;
            case R.id.adetail_tv_responsepic://定责照片
                EntityBean[] beans = dataBean.getBeans("accidentimagelist");
                if (beans == null) {
                    UiUtil.showToast(mContext, "暂无定责照片信息");
                    return;
                }
                AccidentPicView rpicView = new AccidentPicView(mContext, beans);
                rpicView.setType(0);
                showForm(rpicView);
                break;
            case R.id.tv_web://认定书
                String kckpUserType = mApplication.getKckpUserType();
                String resbookImaurl = dataBean.getString("resbookImgurl");
                if(TextUtils.isEmpty(resbookImaurl)){
                    UiUtil.showToast(mContext,"暂无认定书");
                }else {
                    WebViewLoad webviewload = new WebViewLoad(mContext,appcaseno,kckpUserType,resbookImaurl,1);
                    FrameworkManager.getInstance().showNewForm(mContext, webviewload);
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void refresh() {

    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        setListener(false);
        mView = null;
        otherInfoTV = null;
        myInfoTV = null;
        respPicTV = null;
        otherBean = null;
        myBean = null;
    }
}

